package com.rxst.msg.webSocket.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    // 创建redis模板
    @Autowired
    private RedisTemplate<String, String> redis;

    public static RedisTemplate<String, String> stringRedisTemplate;

    @PostConstruct
    public void getRedisTemplate() {
        stringRedisTemplate = this.redis;
        System.out.println("reids:"+stringRedisTemplate);
        set("1234","123123");
    }


    // 构造
    public RedisUtil() {

    }

    /**
     * 获取所有的键
     *
     * @param key
     * @return
     */
    public Set<String> keys(String key) {
        return this.stringRedisTemplate.keys(key);
    }

    /**
     * 通过键名，获取键值对象
     *
     * @param key
     * @return
     */
    public Object get(String key) {
        return this.stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 存键值对象
     *
     * @param key
     * @param value
     */
    public void set(String key, String value) {
        this.stringRedisTemplate.opsForValue().set(key, value, 7200L, TimeUnit.SECONDS);
    }

    /**
     * 存键值对象
     *
     * @param key
     * @param value
     * @param expire 时间(秒) expire要大于0 如果expire小于等于0 将设置无限期
     */
    public void set(String key, String value, Integer expire) {
        this.stringRedisTemplate.opsForValue().set(key, value, (long) expire, TimeUnit.SECONDS);
    }

    public void delete(String key) {
        this.stringRedisTemplate.opsForValue().getOperations().delete(key);
    }

    /**
     * HashSet 向一张hash表中放入数据,如果不存在将创建
     *
     * @param key    键
     * @param filed  值
     * @param domain
     */
    public void hset(String key, String filed, Object domain) {
        this.stringRedisTemplate.opsForHash().put(key, filed, domain);
    }

    /**
     * @param key
     * @param filed
     * @param domain
     * @param expire 如果已存在的hash表有时间,这里将会替换原有的时间
     */
    public void hset(String key, String filed, Object domain, Integer expire) {
        this.stringRedisTemplate.opsForHash().put(key, filed, domain);
        this.stringRedisTemplate.expire(key, (long) expire, TimeUnit.SECONDS);
    }

    /**
     * hashSet
     *
     * @param key 键
     * @param hm  多个键值
     */
    public void hset(String key, HashMap<String, Object> hm) {
        this.stringRedisTemplate.opsForHash().putAll(key, hm);
    }

    public void hsetAbsent(String key, String filed, Object domain) {
        this.stringRedisTemplate.opsForHash().putIfAbsent(key, filed, domain);
    }

    /**
     * @param key
     * @param field
     * @return
     */
    public Object hget(String key, String field) {
        return this.stringRedisTemplate.opsForHash().get(key, field);
    }

    public Object hget(String key) {
        return this.stringRedisTemplate.opsForHash().entries(key);
    }

    public void hdel(String key, String filed) {
        this.stringRedisTemplate.boundHashOps(key).delete(filed);
    }

    /**
     * 删除缓存键值对
     *
     * @param key
     */
    public void deleteKey(String key) {
        this.stringRedisTemplate.opsForHash().getOperations().delete(key);
    }

    public Boolean hasKey(String key, String field) {
        return this.stringRedisTemplate.opsForHash().hasKey(key, field);
    }

    /**
     * 判断key是否存在
     *
     * @param key
     * @return
     */
    public Boolean hasKey(String key) {
        return this.stringRedisTemplate.opsForHash().getOperations().hasKey(key);
    }


}
